﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Runtime.InteropServices;

namespace CodeBuilder.DevTools
{
    public partial class ColorToolForm : UserControl
    {
        public ColorToolForm()
        {
            InitializeComponent();
        }

        private void ColorToolForm_Load(object sender, EventArgs e)
        {
          
        }


        public bool TimerEnable
        {
            set
            {
                this.timer1.Enabled = value;
            }
        }

        public void ColorDisplayInit()
        {
            try
            {
                ColorInit();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private IntPtr hdcDest;
        private IntPtr hdcSrc;
        private Graphics gh;
        private Pen p;

        private Point newPt;
        private int xClient;
        private int xPos;
        private int yClient;
        private int yPos;

        private int multipl = 1;
 

        private void ColorInit()
        {
            this.hdcDest = GetDC(this.picPanel.Handle);
            this.hdcSrc = GetDC(new IntPtr());
            this.gh = this.picPanel.CreateGraphics();
            this.p = new Pen(System.Drawing.Color.Green, 1f);
        }

        [DllImport("user32.dll")]
        internal static extern IntPtr GetDC(IntPtr hwnd);

        private void R_Scroll_Scroll(object sender, ScrollEventArgs e)
        {
            this.ScrollChange();
        }

        private void ScrollChange()
        {
            this.txtColorR.Text = this.R_Scroll.Value.ToString();
            this.txtColorG.Text = this.G_Scroll.Value.ToString();
            this.txtColorB.Text = this.B_Scroll.Value.ToString();
        }

        private void R_Scroll_ValueChanged(object sender, EventArgs e)
        {
            this.ScrollChange();
        }

        private void txtColorCode_DoubleClick(object sender, EventArgs e)
        {
            string text = this.txtColorCode.Text;
            if (text != "")
            {
                Clipboard.Clear();
                Clipboard.SetDataObject(text);
                MessageBox.Show("已经复制到粘贴板");
            }
        }


        private void txtColorCode_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode != Keys.ControlKey)
            {
                e.Handled = true;
            }
        }

        private void txtColorG_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!(char.IsNumber(e.KeyChar) || (e.KeyChar == '\b')))
            {
                e.Handled = true;
            }
        }

        private void txtColorR_TextChanged(object sender, EventArgs e)
        {
            this.InputChangeColoPic();
        }

        private void InputChangeColoPic()
        {
            int red = Convert.ToInt32(this.txtColorR.Text);
            int green = Convert.ToInt32(this.txtColorG.Text);
            int blue = Convert.ToInt32(this.txtColorB.Text);
            if ((((red >= 0) && (red <= 0xff)) && ((green >= 0) && (green <= 0xff))) && ((blue >= 0) && (blue <= 0xff)))
            {
                this.R_Scroll.Value = red;
                this.G_Scroll.Value = green;
                this.B_Scroll.Value = blue;
                System.Drawing.Color color = System.Drawing.Color.FromArgb(red, green, blue);
                string str = this.ColorToHex(color);
                this.txtColorCode.Text = str;
                this.lblColorPic.Text = str;
                this.lblColorPic.ForeColor = color;
                this.lblColorPic.BackColor = this.ToColor((str == "#FFFFFF") ? "#000000" : "#FFFFFF");
                this.colorPic.BackColor = color;
            }
        }


        public string ColorToHex(System.Drawing.Color _color)
        {
            return ColorTranslator.ToHtml(_color);
        }

        public System.Drawing.Color ToColor(string color)
        {
            return ColorTranslator.FromHtml(color);
        }

        [DllImport("gdi32.dll")]
        internal static extern IntPtr StretchBlt(IntPtr hdcDest, int xDest, int yDest, int DestWidth, int DestHeight, IntPtr hdcSrc, int xSrc, int ySrc, int SrcWidth, int SrcHeight, ulong dwRop);

        private void timer1_Tick(object sender, EventArgs e)
        {
            this.newPt = Cursor.Position;
            this.xClient = 200 / this.multipl;
            this.yClient = 200 / this.multipl;
            if (this.newPt.X <= (this.xClient / 2))
            {
                this.xPos = 0;
            }
            else if (this.newPt.X >= (Screen.PrimaryScreen.Bounds.Width - (this.xClient / 2)))
            {
                this.xPos = Screen.PrimaryScreen.Bounds.Width - this.xClient;
            }
            else
            {
                this.xPos = this.newPt.X - (this.xClient / 2);
            }
            if (this.newPt.Y <= (this.yClient / 2))
            {
                this.yPos = 0;
            }
            else if (this.newPt.Y >= (Screen.PrimaryScreen.Bounds.Height - (this.yClient / 2)))
            {
                this.yPos = Screen.PrimaryScreen.Bounds.Height - this.yClient;
            }
            else
            {
                this.yPos = this.newPt.Y - (this.yClient / 2);
            }
            StretchBlt(this.hdcDest, 0, 0, 200, 200, this.hdcSrc, this.xPos, this.yPos, this.xClient, this.yClient, 0xcc0020L);
            this.gh.DrawLine(this.p, (this.newPt.X - this.xPos) * this.multipl, 0, (this.newPt.X - this.xPos) * this.multipl, 200);
            this.gh.DrawLine(this.p, 0, (this.newPt.Y - this.yPos) * this.multipl, 200, (this.newPt.Y - this.yPos) * this.multipl);
        }


        [DllImport("gdi32.dll")]
        internal static extern uint GetPixel(IntPtr hdc, int xPos, int yPos);

        public void colorForm_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.ControlKey)
            {
                Color color = ColorTranslator.FromWin32((int)GetPixel(this.hdcSrc, Cursor.Position.X, Cursor.Position.Y));
                string str = this.ColorToHex(color);
                this.txtColorCode.Text = str;
                this.lblColorPic.Text = str;
                this.lblColorPic.ForeColor = color;
                this.lblColorPic.BackColor = this.ToColor((str == "#FFFFFF") ? "#000000" : "#FFFFFF");
                this.colorPic.BackColor = color;
                this.txtColorR.Text = color.R.ToString();
                this.txtColorG.Text = color.G.ToString();
                this.txtColorB.Text = color.B.ToString();
                this.R_Scroll.Value = color.R;
                this.G_Scroll.Value = color.G;
                this.B_Scroll.Value = color.B;
            }
        }



    }
}
